<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Wants extends MY_Controller {

	function __construct() {
		parent::__construct();
	}
	
	public function index()
	{
		if ($this->isSessionActive()) {
		
			$this->load->model("Wants_model");
			$this->load->model("Browse_model");
			$this->load->model("Home_model");
			
			$data['categories'] = $this->Browse_model->get_categories();
			
			$data['searchresult'] = $this->Wants_model->search();
			
			$data['offers'] = $this->Home_model->get_offers($this->fb_id);
			
			$data['user'] = $this->fb_id;
			$data['title'] = 'Wants';
			$data['content_view'] = 'wants_view';
			$data['stylesheets'] = array('wants','paginate-style');
			$data['javascripts'] = array('wants','paginate', 'wants_paginate', 'slider');

    		$this->load_template_view($data, TRUE);
		}
	}
	
	function submitwant()
	{
		$this->load->model("Wants_model");
		$cat = $_POST['category'];
		$desc = $_POST['desc'];
		
		$this->Wants_model->insertwant($cat, $desc, $this->fb_id);
	}
	
	function search_result()
	{
		$this->load->model("Wants_model");
		$data = $this->Wants_model->search($this->fb_id, $_POST);
		echo json_encode($data);
	}
	
	function getrelatedoffers()
	{
		$want_id = $_POST['want_id'];
		$this->load->model("Wants_model");
		$want = $this->Wants_model->get_want($want_id);
		
		$relatedwants = $this->Wants_model->get_related_wants($this->fb_id, $want['category_id']);
		
		echo json_encode($relatedwants);
	}
	
	function offersuggest()
	{
		$offer_id = $_POST['offer_id'];
		$want_id = $_POST['want_id'];
		$this->load->model("Wants_model");
		$this->load->model("Itemdescription_model");
		$this->load->model("Notification_model");
		$offer = $this->Itemdescription_model->get_offer($offer_id);
		$want = $this->Wants_model->get_want($want_id);
		
		$message = "<a href='".base_url('/profile/')."/".$offer['fb_id']."'><span class='get-fb-name'>".$offer['fb_id']."</span></a> suggests you have a look at his/her <a href='".base_url('/itemdescription/')."/".$offer['offer_id']."'>".$offer['item_title']."</a>. You may be interested in it!";
		$this->Notification_model->create_notification($want['fb_id'], $message);
	}
	
	function delete()
	{
		$want_id = $_POST['want_id'];
		$this->load->model("Wants_model");
		$this->Wants_model->delete($want_id);
	}
}